#include "indexrecord.h"
#include <sstream>

const int KEY = 0;
const int POINTER = 1;


int IndexRecord:: Compare (Attribute &lowVal, Attribute &highVal, Type &myType)
{
	int ret = 0;
	
	// is it equal to the lower bound
	ret = m_vMyAtts[KEY].Compare(lowVal, myType, 0);
	if(ret == 1)
		return 0;

	// Check and see if the lowVal is greater (falls below the range)
	ret = m_vMyAtts[KEY].Compare(lowVal, myType, 1);
	if(ret == 1)
	{
		// is it equal to the upper bound
		ret = m_vMyAtts[KEY].Compare(highVal, myType, 0);
		if(ret == 1)
			return 0;
			
		// Check and see if it falls above the range
		ret = m_vMyAtts[KEY].Compare(highVal, myType, 1);
		if(ret == 1)
			return 1;
		else
			return 0;
	}
	else
	{
		return -1;
	}
	
	
}

void IndexRecord:: Load (char *key, int keyLen, int pointer)
{
	string skey(key);
	
	Attribute atKey;
	atKey.SetData(skey, String);
	atKey.Free();
	
	string strPointer;	//turn the number to a string
	stringstream out;
	out << pointer;
	strPointer = out.str();
	
	
	Attribute atPointer;
	atPointer.SetData(strPointer, Int);
	atPointer.Free();
	
	m_vMyAtts.push_back(atKey); // First inserted a key, 
	m_vMyAtts.push_back(atPointer);	//inserted the pointer to the page
	
	m_iNumAtts += 2;
}


void IndexRecord:: GetKey (Attribute & _at)
{
	_at = m_vMyAtts[KEY];
	
}

int IndexRecord:: GetPointer ()
{
	char *pointer = new char[sizeof(int)];
	
	m_vMyAtts[POINTER].FromBinary(pointer);
	int pi = atoi(pointer);
	delete [] pointer;
	return pi;
}

